package com.bdyc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bdyc.controller.custom.ManagerInfoPo;
import com.bdyc.controller.custom.MapVo;
import com.bdyc.controller.custom.MyPo;
import com.bdyc.po.ManagerInfo;
import com.bdyc.service.spring.ManagerInfoService;
import com.bdyc.util.AppConstant;

@Controller
@RequestMapping("/managerInfo")
public class ManagerController {
	
	@Autowired(required=true)
	@Qualifier("managerInfoService")
	private ManagerInfoService managerInfoServiceImpl;
	
	//添加manager方法
	@RequestMapping(value="/addManagerInfo.action",method=RequestMethod.POST)
	@ResponseBody//返回json数据注解
	public Map<String,Object> addManagerInfo(ManagerInfo managerInfo,Integer roleId){
		Map<String,Object> map = new HashMap<String,Object>();
		try{
				//插入manager
				managerInfoServiceImpl.addManagerInfo(managerInfo,roleId);
				map.put(AppConstant.ISSUCC, AppConstant.SUCCESS);
				map.put(AppConstant.MSG, AppConstant.ADDSUCCESS);
				return map;
		}catch(Exception e){
			e.printStackTrace();
			map.put(AppConstant.ISSUCC,AppConstant.FALL);
			map.put(AppConstant.MSG,AppConstant.ADDFALL);
			return map;
		}
	}
	//判断客户管理员登录名是否唯一方法入口,注册时可判断是否用户名重复
	@RequestMapping(value="/onlyName.action",method=RequestMethod.POST)
	@ResponseBody//返回json数据注解
	public Map<String,Object> onlyName(String name){
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			//查询managerInfo表 判断用户名是否存在
			Integer count = managerInfoServiceImpl.selectName(name);
			if(count >0){
				map.put(AppConstant.ISSUCC,AppConstant.FALL);
				map.put(AppConstant.MSG,"用户名不可用!");
			}else{
				map.put(AppConstant.ISSUCC, AppConstant.SUCCESS);
				map.put(AppConstant.MSG,"用户名可用!");
			}
			return map;
		}catch(Exception e){
			e.printStackTrace();
			map.put(AppConstant.MSG, "服务器异常，请稍后重试!");
			return map;
		}
	}
	//登录方法
	@RequestMapping(value="/login.action",method=RequestMethod.GET)
	@ResponseBody//返回json数据注解
	public Map<String,Object> login(String name,String pass,HttpSession session){
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			ManagerInfoPo managerInfoRole = managerInfoServiceImpl.login(name,pass);
			if(managerInfoRole != null){
				session.setAttribute("info", managerInfoRole);
				switch (managerInfoRole.getRid()) {
				case 1:
					map.put("msg", 1);//"系统管理员"
					break;
				case 2:
					map.put("mag", 2);//"运维"
					break;
				case 3:
					map.put("mag", 3);//"客服"
					break;
				default:
					break;
				}
				map.put(AppConstant.ISSUCC,AppConstant.SUCCESS);
			}else{
				map.put(AppConstant.ISSUCC,AppConstant.FALL);
				map.put(AppConstant.MSG, "用户名或密码错误!请重新登录!");
			}
			return map;
		}catch(Exception e){
			e.printStackTrace();
			map.put(AppConstant.ISSUCC,AppConstant.FALL);
			map.put(AppConstant.MSG, "服务器异常，请稍后重试!");
			return map;
		}
	}
	//查询所有managerInfo
	@RequestMapping(value="/findManagerInfoList.action",method=RequestMethod.GET)
	@ResponseBody//返回json数据注解
	public Map<String,Object> findManagerInfoList(MyPo po){
		Map<String,Object> map = new HashMap<String,Object>();
		//封装分页查询map
		//封装分页查询总记录数map
		Map<String,Object> sendMap = MapVo.getSendMap(po);
		Integer rows = (Integer)sendMap.get("rows");
		try{
			List<ManagerInfoPo> infoList = managerInfoServiceImpl.findUserInfoList(sendMap);
			Integer count = managerInfoServiceImpl.findUserInfoListCount(sendMap);
			Integer index = count/rows;
			Integer sum = count%rows==0?index:index +1;
			map.put("sum", sum);
			map.put("count", count);
			map.put("info", infoList);
			map.put(AppConstant.ISSUCC,AppConstant.SUCCESS);
			map.put(AppConstant.MSG,AppConstant.FINDSUCCESS);
			return map;
		}catch(Exception e){
			e.printStackTrace();
			map.put(AppConstant.ISSUCC,AppConstant.FALL);
			map.put(AppConstant.MSG,AppConstant.FINDFALL );
			return map;
		}
	}
}
